by CM
Posted on November 26, 2021
In this article, we will build a Firebase application that holds all lottery jackpots (the winning 6 numbers) in Germany since 1955. Having the final application, we provide the user the possibility to enter 6+ numbers and 'hopefully' find a draw / lotto jackpot from the past that matches his numbers. Spoiler alert - it is still fairly difficult to hit 6 numbers even when including all jackpots ever. To put this into perspective, there are about 5000 drawings since 1955. In the draw, six numbers are drawn from one machine (containing 1 to 49) - for simplicity reasons we will ignore the Superzahl that is drawn from a second machine (containing 0-9) which has been introduced in 2013. As a bonus we provide the hypothetical Winnings in EURO currency for the particular draw. Regarding the code, we will build the entire application using vanilla JavaScript and HTML with Bootstrap. Lastly, we will deploy our application on Firebase hosting.
Let's jump right into the Code. Note that we will only highlight the JavaScript part in the article. The html and css is straightforward and can be found in the GitHub repo. In the first step, we will create 49 input boxes via html. We then create an add_to_array function that allows us to add the clicked box value to an all_numbers array
var all_numbers = [];
function add_to_array() {
all_numbers = [];
if (document.getElementById('checkbox_1').checked) {
all_numbers.push(document.getElementById('checkbox_1').value)
};
if (document.getElementById('checkbox_2').checked) {
all_numbers.push(document.getElementById('checkbox_2').value)
};
....
if (document.getElementById('checkbox_49').checked) {
all_numbers.push(document.getElementById('checkbox_49').value)
};
Second, we set up our variables to pinpoint (1) the number of hits, (2) the winning numbers, (3) the prize money, and the (4) matched win aka the 'draw from a particular date'.
function myFunction()
{
guess = all_numbers;
console.log(guess);
match = [];
var treffer = 0;
var gewinnzahlen = [];
var match_win = [];
var money = [];
//Sorting Array ASCENDING for easier iteration
all_numbers.sort(function(a, b) {
return a - b;
});
Third, we will iterate over all draws.Note that we need to iterate over all numbers as well as over all rows respectively all individual draws.
// Iterating over ALL DRAWS
for (var i = 0, l1 = ziehung.length; i < l1; i++) {
for (var j = 0, l2 = ziehung[i].length; j < l2; j++) {
treffer = 0;
gewinnzahlen = [];
So let's now see whether we got the jackpot by checking the individual drawing and pushing the winning numbers in the 'Gewinnzahlen' array
if (all_numbers.length >= 6) {
if (all_numbers[0] == ziehung[i][j] ||
all_numbers[0] == ziehung[i][j + 1] ||
all_numbers[0] == ziehung[i][j + 2] ||
all_numbers[0] == ziehung[i][j + 3] ||
all_numbers[0] == ziehung[i][j + 4] ||
all_numbers[0] == ziehung[i][j + 5]) {
treffer = treffer + 1;
gewinnzahlen.push(all_numbers[0]);
};
...
if (all_numbers.length >= 49) {
if (all_numbers[48] == ziehung[i][j] ||
all_numbers[48] == ziehung[i][j + 1] ||
all_numbers[48] == ziehung[i][j + 2] ||
all_numbers[48] == ziehung[i][j + 3] ||
all_numbers[48] == ziehung[i][j + 4] ||
all_numbers[48] == ziehung[i][j + 5]) {
treffer = treffer + 1;
gewinnzahlen.push(all_numbers[48]);
};
};
In case we get 6 hits - we print out the winning numbers, the jackpot match aka date of the draw, and the prize money. We also add the right currency to the output and format the numbers accordingly.
if (treffer >= 6) {
document.getElementById("table_div").style.display = "block";
match.push(ziehung[i][0]);
money.push(ziehung[i][7]);
match_win.push(ziehung[i][0], gewinnzahlen);
document.getElementById('results').innerHTML = match_win.length / 2 + "x 'JACKPOT'";
let sum = 0;
for (let ix = 0; ix < money.length; ix++) {
sum += money[ix];
}
var formatter = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR',
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});
document.getElementById('dein_gewinn').innerHTML ="Gewinn: " +
formatter.format(Math.round(sum));
var match_i = 1;
let txt_date = "";
let txt_zahl = "";
match.forEach(myX);
match.forEach(myY);
document.getElementById("date").innerHTML = txt_date;
function myX(value) {
txt_date += "<tr>" + value + "</tr></br>";
};
document.getElementById("date_zahlen").innerHTML = txt_zahl;
function myY(value) {
txt_zahl += match_win[0 + match_i] + "</tr></br>";
match_i = match_i + 2
};
};
};
}
}
}
Done!
Next step is to put our app on Firebase. Therefore, we go into our project folder and open the console. We initialize a firebase project via:
firebase init
Next, we select hosting in the option menu:
We then set out public directory - aka the directory where all the files are located the we want to deploy.
For our app - a single-page app is the right choice. In addition, we do not want to set up automatic build and deploy with GitHub. Lastly, make sure to not overwrite your html index file.
It should only take a couple of seconds until the app is deployed. We also receive our free hosting url to reach the app via the web.
Success! [Did you ever win the Lottery Jackpot?] Lotto.Insights.de #Firebase is amazing for creating web-apps! For educational purposes, we just developed a small Lotto-Insights application that allows you to evaluate your potential Lottery Jackpot over all lottery draws in Germany since 1955. The application is hosted on Firebase hosting, hence cached on SSD and served via a global CDN with SSL certification. Firebase is a backend as a service that allows you to build and run a production app at scale. Lastly, I got a custom domain from #Google #Domains that redirects to the Firebase application. >> (https://lotto-insights.de/)